﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DAL;

namespace BLL
{
  public class ContactBLL
  {
    public List<Contact> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Contacts.OrderByDescending(p => p.DateSend).ToList();
      }
    }

    /// <summary>
    /// Sort = 1 ASC
    /// Sort = 2 DESC
    /// </summary>
    /// <param name="sort"></param>
    /// <returns></returns>
    public List<Contact> GetAll(int sort)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        if (sort == 1)
          return data.Contacts.OrderByDescending(p => p.Id).ToList();
        else
          return data.Contacts.OrderBy(p => p.Id).ToList();
      }
    }

    public List<Contact> GetAll(bool isRead)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
         return data.Contacts.Where(p => p.isRead == isRead).ToList();
      }
    }

    public Contact GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Contacts.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public List<Contact> GetByRead(bool isRead)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Contacts.Where(p => p.isRead == isRead).ToList();
      }
    }
    /// <summary>
    /// Sort = 1 ASC
    /// Sort = 2 DESC
    /// </summary>
    /// <param name="isRead"></param>
    /// <param name="sort"></param>
    /// <returns></returns>
    public List<Contact> GetByRead(bool isRead,int sort)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        if (sort == 1)
          return data.Contacts.Where(p => p.isRead == isRead).OrderByDescending(p => p.Id).ToList();
        else
          return data.Contacts.Where(p => p.isRead == isRead).OrderBy(p => p.Id).ToList();
      }
    }

    public void Read(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Contact obj = data.Contacts.Where(p => p.Id == id).FirstOrDefault();
        obj.isRead = true;

        data.SubmitChanges();
      }
    }

    public void Add(Contact obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Contacts.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Contact obj = data.Contacts.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
          data.Contacts.DeleteOnSubmit(obj);

        data.SubmitChanges();
      }
    }
  }
}
